<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Expense_List extends Page
{
       public $btnSearch = null;
       public $edtSearch = null;
       public $Image4 = null;
       public $lblPlateNum = null;
       public $Label49 = null;
       public $lblmodel = null;
       public $lblmake = null;
       public $lblyr = null;
       public $Label43 = null;
       public $Label38 = null;
       public $lblyear = null;
       public $Label48 = null;
       public $lblVeh = null;
       public $btnExport = null;
       public $JTExpenseHidden = null;
       public $JTJavaScript1 = null;
       public $imgBack = null;
       public $Image3 = null;
       public $imgAddExpense = null;
       public $hiddenId = null;
       public $JTPlatinumVehicleExpense = null;
       public $JTSiteTheme1 = null;


       function edtSearchJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edtSearch').value = '';
       <?php

       }

       function btnExportJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['btnExport'].src="image/excel.png"
       <?php

       }

       function btnExportJSMouseOver($sender, $params)
       {

       ?>
       document.images['btnExport'].src="image/excel2.png"

       <?php

       }

       function btnExportClick($sender, $params)
       {
       $this->JTExpenseHidden->exportGridToXLSDownload('expense_list.xls');


       }

       function JTExpenseHiddenShow($sender, $params)
       {
       $veh_id = $this->hiddenId->getValue();

       $query_veh = "select * from vehicle_tbl where vehicle_id='".$this->hiddenId->getValue()."'";
       $result_veh = mysql_query($query_veh)or die(mysql_error());
       $row_veh = mysql_fetch_array($result_veh);

       $query1 = "select * from veh_expense_tbl where vehicle_id='".$veh_id."'";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();

       $arr[] = array(
                        '1' => 'Vehicle Num:'.' '.$row_veh['VIN'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );


       $arr[] = array(
                        '1' => 'Plate Num:'.' '.$row_veh['veh_num'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => 'Year:'.' '.$row_veh['year'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => 'Make:'.' '.$row_veh['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => 'Model:'.' '.$row_veh['model_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => 'Type/Style:'.' '.$row_veh['style_name'],
                        '2'=> ' ',
                        '3' => ' ',
                        '4' => ' '
                    );

       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );


       $arr[] = array(
                        '1' => 'WORKORDER - PARTS REPORT',
                        '2'=>' ',
                        '3' => ' ',
                        '4' => ' '
                    );

      $arr[] = array(
      '1' => 'Date',
      '2'=> 'Description',
      '3' => 'Type',
      '4' => 'Amt'
      );




       while($row = mysql_fetch_array($result))
       {

       $date = "<a href=veh_expense_edit.php?veh=".$veh_id."&expense=".$row['payment_id']." title='edit expense'>".$row['date']."</a>";
       $tools = "<img onclick=js_deleteExpense('".$veh_id."','".$row['payment_id']."'); src='image/delete_orange.jpg' title='delete'>";

       $arr[] = array(
                        '1' => $row['date'],
                        '2' => $row['description'],
                        '3' => $row['type'],
                        '4' => $row['amount']
                        );
       }

      $total_amount = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_expense_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_amount = mysql_fetch_array($total_amount);


      $arr[] = array(
                        '1' => TOTAL,
                        '4' => $row_amount['sum_amount'],
                        );

       $this->JTExpenseHidden->setCellData($arr);


       }

       function imgAddExpenseJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgAddExpense'].src="image/add_new.png"
       <?php

       }

       function imgAddExpenseJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgAddExpense'].src="image/add_new_blue.png"
       <?php

       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }


       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }



       function Vehicle_Expense_ListCreate($sender, $params)
       {
       if($_SESSION['isAdmin']!=1)
       {
        $this->imgAddExpense->setVisible(false);
        $this->btnExport->Left = 943;
       }

       if($_SESSION['isAdmin']==1)
       {
        $this->imgAddExpense->setVisible(true);
        $this->btnExport->Left = 838;
       }

       $id = $_GET['veh'];
       if(isset($id) && ($id!='' || $id!=null))
       $this->hiddenId->setValue($id);

       $result_vin = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='" .$this->hiddenId->getValue(). "'");
       $row_selected = mysql_fetch_array($result_vin);
       $label = $row_selected['VIN'];
       $this->lblVeh->Caption = $label;
       $this->lblyr->Caption = $row_selected['year'];
       $this->lblmake->Caption = $row_selected['manufacturer_name'];
       $this->lblmodel->Caption = $row_selected['model_name'];
       $this->lblPlateNum->Caption = $row_selected['veh_num'];

       $this->imgBack->setLink('vehicle_list.php?company='.$_SESSION['company_id']);
       $this->imgAddExpense->setLink('veh_expense_create.php?veh='.$this->hiddenId->getValue());


       }


       function JTPlatinumVehicleExpenseShow($sender, $params)
       {
       $veh_id = $this->hiddenId->getValue();
       $query1 = "select * from veh_expense_tbl where vehicle_id='".$veh_id."' and (date like '%".$this->edtSearch->Text."%'
        or description like '%".$this->edtSearch->Text."%'
        or type like '%".$this->edtSearch->Text."%') order by date DESC";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();
       while($row = mysql_fetch_array($result))
       {

       $date = "<a href=veh_expense_edit.php?veh=".$veh_id."&expense=".$row['payment_id']." title='edit expense'>".$row['date']."</a>";
       $tools = "<img onclick=js_deleteExpense('".$veh_id."','".$row['payment_id']."'); src='image/delete.png' title='delete'>";

       $amt = $row['amount'];
       $camt = $this->commafy($amt);
       $div_camt = "<div style='text-align:right'>".$camt."</div>";

       if($_SESSION['isAdmin']==1)
       {
       $arr[] = array(
                        'Date' => $date,
                        'Description' => $row['description'],
                        'Type' => $row['type'],
                        'Amount' => $div_camt,
                        'Tools' => $tools
                        );
       }
       if($_SESSION['isAdmin']!=1)
       {
       $arr[] = array(
                        'Date' => $row['date'],
                        'Description' => $row['description'],
                        'Type' => $row['type'],
                        'Amount' => $div_camt
                        );
       }

       }

      $total_amount = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_expense_tbl where vehicle_id='".$this->hiddenId->getValue()."'");
      $row_amount = mysql_fetch_array($total_amount);

      $tot = $row_amount['sum_amount'];
      $ctot = $this->commafy($tot);
      //$align = "<table><tr><td style='text-align:right'>".$ctot."</td></tr></table>";
      $div_ctot = "<div style='text-align:right'>".$ctot."</div>";
      $align = "<font style='text-align:right'>".$ctot."</font>";
      $ctot_tbl = "<table width = '100%'><tr><td style='text-align:right'>".$ctot."</td></tr></table>";
      //"<table width = '100%'><tr><td align = 'right'>".$ctot."</td></tr></table>"

      $arr[] = array(
                        'Date' => TOTAL,
                        'Amount' => $div_ctot
                        );

       $this->JTPlatinumVehicleExpense->setCellData($arr);

       }

}

global $application;

global $Vehicle_Expense_List;

//Creates the form
$Vehicle_Expense_List=new Vehicle_Expense_List($application);

//Read from resource file
$Vehicle_Expense_List->loadResource(__FILE__);

//Shows the form
$Vehicle_Expense_List->show();

?>